package cn.com.header.web.controller.sys;


import cn.com.header.core.util.ApplicationUtils;
import cn.com.header.core.util.LangUtils;
import cn.com.header.core.util.Page;
import cn.com.header.core.util.SysProperties;
import cn.com.header.web.model.sys.Role;
import cn.com.header.web.model.sys.User;
import cn.com.header.web.service.sys.RoleService;
import cn.com.header.web.service.sys.UserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 * 
 * @author StarZou
 * @since 2014年5月28日 下午3:54:00
 **/
@Controller
@RequestMapping(value = "/sys/user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private RoleService roleSvc;


    /**
     * 基于权限标识的权限控制案例
     */
    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Role> roleList=roleSvc.selectAll();
        request.setAttribute("roleList",roleList);//把所有角色显示出来
        return "sys/user/list";
    }


    /**
     * 基于权限标识的权限控制案例
     */
    @RequestMapping(value = "/context")
    public String context(@RequestParam(defaultValue = "1")Integer pageNo, String userName, String trueName, Long roleId, HttpServletRequest request) {
        Page page=new Page(pageNo);
        page.addCond("userName", userName);
        page.addCond("trueName", trueName);
        page.addCond("roleId", roleId);
/*        Long currentRoleId = (Long) request.getSession().getAttribute("sessionRoleId");
        if (LangUtils.isNotEmpty(currentRoleId)){
            page.addCond("currentRoleId", currentRoleId);
        }*/
        userService.selectByExampleAndPage(page);
        request.setAttribute("page",page);
        return "sys/user/context";
    }

    /**
     * 查看资料
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/show")
    public String show(Long id, HttpServletRequest request) throws Exception{
        if (LangUtils.isNotEmpty(id)){
            User user = userService.get(id);
            request.setAttribute("vo", user);
            return "sys/user/show";
        }
        return null;
    }

    @RequestMapping(value = "/edit")
    public String edit(Long id, HttpServletRequest request) throws Exception{
        if (LangUtils.isNotEmpty(id)){
            User user = userService.get(id);
            request.setAttribute("vo", user);
        }
        return "sys/user/edit";
    }
    @RequestMapping(value = "/save")
    public String save(User user, HttpServletRequest request) throws Exception{
        if (LangUtils.isNotEmpty(user.getId())){
            User userVo = userService.get(user.getId());
            userVo.setUserName(user.getUserName());
            userVo.setTrueName(user.getTrueName());
            userVo.setGxsj(new Date());
            userService.update(userVo);
            //此处未做其他相关角色表与用户表关联，实际项目中应该还要更新相关角色的实体表数据
        }
        return "redirect:/sys/user/list.do";
    }

    @ResponseBody
    @RequestMapping(value = "/check")
    public String check(Long id, String userName, HttpServletRequest request) throws Exception{
        JSONObject obj = new JSONObject();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("userName", userName);
        map.put("delMark", SysProperties.delMark.STATE_F);
        boolean flag = userService.check(map);
        obj.put("valid", flag);
        return obj.toString();
    }

    /**
     * 用户删除
     * @param id
     * @param response
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public String delete(Long id, HttpServletResponse response){
        JSONObject obj = new JSONObject();
        try {
            if (LangUtils.isNotEmpty(id)) {
                userService.deleteUser(id);
                obj.put("msg", true);
            }
        }
        catch(Exception e){
            obj.put("msg", false);
        }
        return obj.toString();
    }
    @RequestMapping(value = "/editPwd")
    public String editPwd(Long id, HttpServletRequest request){
        if (LangUtils.isNotEmpty(id)){
            User user = userService.get(id);
            request.setAttribute("vo", user);
        }
        return "sys/user/editPwd";
    }

    @RequestMapping(value = "/savePwd")
    public String savePwd(Long id, @RequestParam(name = "password") String password, HttpServletRequest request, HttpServletResponse response) throws Exception{
        User user = userService.get(id);
        user.setUserPwd(ApplicationUtils.sha256Hex(password));
        userService.update(user);
        return "redirect:/sys/user/list.do";
    }

}
